<?php

  require_once ("../accesobd.php");
 

include (dirname(dirname(__FILE__)).'/Guardado/crearpdf2.php');

    $id = $_POST["id"];
    $fechaini = $_POST["fechaini"];
    $fechafin = $_POST["fechafin"];
    
    
    $fe_cmp = $_POST["fecha_cmp"];

if ($fe_cmp){
            $suma=0;
         
            $tabla= pg_query($conex, "select to_char(femision, 'YYYY') from factura;");
            
            while ($fecha_array = pg_fetch_array($tabla)){
                if (strcmp($fecha_array[0], $fe_cmp) == 0){
                    $suma++;
                } 
            }
     }
    
        if($id){
             $desven = "select  emp.ciemp,
	                  emp.nombreemp,
	                  emp.apemp,
	                  emp.diremp,
	                  veh.idveh,
	                  veh.modelo,
	                  fact.femision,
	                  fact.precioventa,
                          5*(fact.precioventa)/100 as com,
	                  sum(fact.precioventa),
	                  count(fact.precioventa) 
             from 	empleado as emp, factura as fact, vehiculo as veh
where  emp.idemp = '$id' and  emp.idemp = fact.idemp and (fact.femision between '$fechaini' and '$fechafin') and veh.idveh = fact.idveh 
group by emp.ciemp,
	emp.nombreemp,
	emp.apemp,
	emp.diremp,
	veh.idveh,
	veh.modelo,
	fact.femision,
	fact.precioventa;";
     }   else{     
               
 
    $desven = "select  emp.ciemp,
	                  emp.nombreemp,
	                  emp.apemp,
	                  emp.diremp,
	                  veh.idveh,
	                  veh.modelo,
	                  fact.femision,
	                  fact.precioventa,
                          5*(fact.precioventa)/100 as com,
	                  sum(fact.precioventa),
	                  count(fact.precioventa) 
             from 	empleado as emp, factura as fact, vehiculo as veh
where   emp.idemp = fact.idemp and (fact.femision between '$fechaini' and '$fechafin') and veh.idveh = fact.idveh 
group by emp.ciemp,
	emp.nombreemp,
	emp.apemp,
	emp.diremp,
	veh.idveh,
	veh.modelo,
	fact.femision,
	fact.precioventa;";
     }
    $query = pg_query($conex,$desven) or die('Consulta fallida: ' . pg_last_error());
        // Redireccion
        pg_close($conex);
        //header("Location: ../../admin/gestionmesas.php")
        
    
    
  
     $cuerpo.= "<img src='../img/logo.png' /> <tr>
                        <td colspan='9' align='center'>Desempeño Vendedores</td></tr><table border=1 >
      
                        <thead>  
                        <tr>
                               



                                <th>Cedula</th>
                                <th>Nombre</th>
                                <th>Apellido</th>
                                <th>Direccion</th>
                                <th>Id Vehiculo</th>
                                <th>Modelo</th>
                                <th>Fecha Emi Fact</th>
                                <th>Precio de Venta</th>
                               <th>Comision</th>
                        </tr>
                      </thead>"?>
               
                 
 
<?php   
        
         while ($linea = pg_fetch_array($query, null, PGSQL_ASSOC)) {
                    //Guardando tuplas en el pdf
             

                    $cuerpo.= "
                        <tr>
                        <td>  ".$linea['ciemp']."</td>
                        <td> ".$linea['nombreemp']."</td>
                        <td> ".$linea['apemp']." </td>
                        <td> ".$linea['diremp']." </td>
                        <td> ".$linea['idveh']."</td>
                        <td> ".$linea['modelo']." </td>
                        <td> ".$linea['femision']."</td>             
                        <td> ".$linea['precioventa']." </td> 
                        <td> ".$linea['com']." </td>      
                        </tr>";
         }  
                 $cuerpo.= "</table>";
              if($fe_cmp){   
                 $cuerpo.= "Ventas por año (".$fe_cmp."): ".$suma." ";
              }
              
              crear_pdf($cuerpo,'Desemp');
    
?>
